package com.jfinal.code.eclipse.plugin.generator.db;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.jfinal.code.eclipse.plugin.generator.dto.DatabaseInfoValueDTO;
/**
 * 仅实现了获取Connection的数据源，只是为了不引入第三方依赖增大插件的体积
 * @author yaolin
 *
 */
public class ConnectionDatasource implements DataSource {

	private  DatabaseInfoValueDTO databaseInfoValueDTO;
	public ConnectionDatasource(DatabaseInfoValueDTO databaseInfoValueDTO) {
		this.databaseInfoValueDTO = databaseInfoValueDTO;
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		throw new RuntimeException("Not Implemented");
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		throw new RuntimeException("Not Implemented");
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		throw new RuntimeException("Not Implemented");
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		throw new RuntimeException("Not Implemented");
	}

//	@Override
//	public Logger getParentLogger() throws SQLFeatureNotSupportedException { // JDK1.7+
//		throw new RuntimeException("Not Implemented");
//	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		throw new RuntimeException("Not Implemented");
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		throw new RuntimeException("Not Implemented");
	}

	@Override
	public Connection getConnection() throws SQLException {
		if (databaseInfoValueDTO != null) {
			try {
				return DbUtils.getConnection(databaseInfoValueDTO.getJarFile(), databaseInfoValueDTO.getDriverClassName(),
					databaseInfoValueDTO.getUrl(), databaseInfoValueDTO.getUsername(), databaseInfoValueDTO.getPassword());
			} catch (Throwable e) {
				throw new SQLException(e);
			}
		}
		return null;
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		throw new RuntimeException("Not Implemented");
	}
}